package com.yan.jingdongshop.services;

import com.yan.jingdongshop.dao.Dao;
import com.yan.jingdongshop.domain.Goods;

import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

/**
 * @author taoge
 * @Date 2024/8/2 18:54
 * @Description
 */
public class ServicesImpl implements Services{
    public static final Scanner INPUT = new Scanner(System.in);
    @Override
    public void addGoods() {
        System.out.println("请输入商品编号");
        String id = INPUT.next();
        System.out.println("请输入商品名称");
        String name = INPUT.next();
        System.out.println("请输入商品价格");
        double price = INPUT.nextDouble();
        System.out.println("请输入商品颜色");
        String colour = INPUT.next();
        System.out.println("请输入商品尺寸");
        int size = INPUT.nextInt();
        System.out.println("请输入商品库存");
        int stoke = INPUT.nextInt();
        Goods goods = new Goods(id, name, price, colour, size, stoke);
        Dao.goods.put(id , goods);
        System.out.println("添加成功");
    }

    @Override
    public void viewAllGoods() {
        System.out.println("编号\t商品名称\t价格\t颜色\t尺寸\t库存");
        Set<String> keySet = Dao.goods.keySet();
        keySet.forEach(item-> System.out.println(Dao.goods.get(item)));
    }

    @Override
    public void viewAppointGoods() {
        System.out.println("请输入要查询的商品编号");
        String Uid = INPUT.next();
        Set<String> keySet = Dao.goods.keySet();
        for (String id : keySet) {
            if(Uid.equals(id)){
                System.out.println("编号\t商品名称\t价格\t颜色\t尺寸\t库存");
                System.out.println(Dao.goods.get(id));
                break;
            }
        }
    }

    @Override
    public void addCart() {
        System.out.println("请输入商品编号");
        String Uid = INPUT.next();
        Set<String> keySet = Dao.goods.keySet();
        for (String id : keySet) {
            if(Uid.equals(id)){
                System.out.println("请输入要购买的商品数量");
                int num = INPUT.nextInt();
                Goods goods = Dao.goods.get(id);
                Set<String> key = Dao.shopCart.keySet();
                for (String cart : key) {
                    if(Dao.shopCart.get(cart).equals(goods)){
                        goods.setStoke(Dao.shopCart.get(cart).getStoke() + num);
                        Dao.shopCart.put(id , goods);
                        System.out.println("添加成功");
                        return;
                    }
                }
                goods.setStoke(num);
                Dao.shopCart.put(id , goods);
                System.out.println("添加成功");
                break;
            }
        }
    }

    @Override
    public void viewCart() {
        Double total = 0.0;
        System.out.println("编号\t商品名称\t价格\t颜色\t尺寸\t库存");
        Set<String> keySet = Dao.shopCart.keySet();
        for (String goods : keySet) {
            System.out.println(Dao.shopCart.get(goods));
            total += Dao.shopCart.get(goods).getPrice() * Dao.shopCart.get(goods).getStoke();
        }
        System.out.println("总计:" + total);
    }
}
